package com.zj.open.wechat.service.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.zj.open.wechat.dao.MessageMapper;
import com.zj.open.wechat.dto.AbstractMessage;
import com.zj.open.wechat.model.Message;

public abstract class MessageProcessor<T extends AbstractMessage> {
    @Autowired
    MessageMapper messageMapper;

    public void handle(T message) {
        if (message instanceof com.zj.open.wechat.dto.msg.Message) {
            Message msg = new Message();
            msg.setMsgId(((com.zj.open.wechat.dto.msg.Message) message).getMsgId());
            msg.setMsgTime(new Date(Long.parseLong(message.getCreateTime())));
            msg.setMsgType(message.getMsgType());
            msg.setMsgContent(JSON.toJSONString(message));
            messageMapper.insert(msg);
        }
        process(message);
    }

    public abstract void process(T message);

}